﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using APNS.NET;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace APNSUnitTests.given_a_notificationpayload
{
    
    public class when_I_serialize_a_notification_payload
    {

        [TestClass]
        public class and_there_is_no_alert_specified
        {
            [TestMethod]
            public void then_an_empty_payload_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();
                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":\"\"}}", payloadString);
            }
        }

        [TestClass]
        public class and_there_is_an_alert_specified
        {
            [TestMethod]
            public void then_a_payload_with_the_alert_message_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();
                payload.AlertBody = "This is a test";
                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":\"This is a test\"}}", payloadString);
            }
        }

        [TestClass]
        public class and_there_is_badge_specified
        {
            [TestMethod]
            public void then_a_payload_with_the_badge_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();
                payload.AlertBody = "This is a test";
                payload.Badge = 4;
                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":\"This is a test\",\"badge\":4}}", payloadString);
            }
        }

        [TestClass]
        public class and_there_is_a_sound_specified
        {
            [TestMethod]
            public void then_a_payload_with_the_sound_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();
                payload.AlertBody = "This is a test";
                payload.Sound = "Sound1";
                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":\"This is a test\",\"sound\":\"Sound1\"}}", payloadString);
            }
        }

        [TestClass]
        public class and_there_are_custom_arguments
        {
            [TestMethod]
            public void then_a_payload_with_the_custom_arguments_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();
                payload.AlertBody = "This is a test";
                payload.CustomArguments["Key1"] = "val1";
                payload.CustomArguments["Key2"] = "val2";

                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":\"This is a test\"},\"Key1\":\"val1\",\"Key2\":\"val2\"}", payloadString);
            }
        }

        [TestClass]
        public class and_the_body_is_an_extended_alert
        {
            [TestMethod]
            public void then_a_payload_with_the_extended_alert_should_be_generated()
            {
                NotificationPayload payload = new NotificationPayload();

                ExtendedApnsAlert extendedAlert = new ExtendedApnsAlert()
                                                      {
                                                          Body = "Test Body",
                                                          LaunchImage = "Image1",
                                                          LocalizableActionKey = "Key1",
                                                          LocalizableKey = "Key2"
                                                      };
                extendedAlert.LocalizableValues.Add("Test1");
                extendedAlert.LocalizableValues.Add("Test2");

                payload.AlertBody = extendedAlert;

                string payloadString = Encoding.UTF8.GetString(payload.ToByteArray());
                Assert.AreEqual("{\"aps\":{\"alert\":{\"body\":\"Test Body\",\"action-loc-key\":\"Key1\",\"loc-key\":\"Key2\",\"loc-args\":[\"Test1\",\"Test2\"],\"launch-image\":\"Image1\"}}}", payloadString);
            }
        }
        
    }
}
